﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.TaxonomySingleValue;
using System.Collections.Generic;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.Support;
using System.Linq;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class TaxonomySingleValue : UserControl, ITaxonomySingleValueView
    {


        #region fields

        string _selectedTerm;
        TaxonomySingleValuePresenter _presenter;

        #endregion

        #region Properties

        public string SeletedTerm
        {
            get{  return ddlTaxonomyField.SelectedIndex == 0 ? string.Empty : ddlTaxonomyField.SelectedItem.Text; }
            set{ _selectedTerm = value; }
        }

        public TaxonomyFieldValue TaxonomySelectedValue
        {
            get { return TermStoreHelper.GetTaxonomyFieldByTerm(TermGroup, TermSet, ddlTaxonomyField.SelectedItem.Text); }
        }

        public string TermSet { get; set; }
        public string TermGroup { get; set; }
        public Dictionary<string, string> TermValues { get; set; }
        public string InitialElement { get; set; } 
        public bool ReadOnly { get; set; }
        public bool isRequired { get; set; }
        public string ValidationGroup { get; set; }

        #endregion

        #region Form Actions

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new TaxonomySingleValuePresenter(this);
            Required(isRequired, ValidationGroup);
            if (IsPostBack) return;
            _presenter.HandleLoad();
        }

        #endregion

        #region Implemented Methods

        public void HandleShow()
        {
            var terms = this.TermValues.ToList();
            if (string.IsNullOrEmpty(InitialElement))
                terms.Insert(0, new KeyValuePair<string, string>("0",TermSet));
            else
                terms.Insert(0, new KeyValuePair<string, string>("0", InitialElement));

            ddlTaxonomyField.DataSource = terms;
            ddlTaxonomyField.DataTextField = "Value";
            ddlTaxonomyField.DataValueField = "Key";
            ddlTaxonomyField.DataBind();

            if (!string.IsNullOrEmpty(_selectedTerm))
                ddlTaxonomyField.SelectedIndex = ddlTaxonomyField.Items.IndexOf((from term in ddlTaxonomyField.Items.OfType<ListItem>()
                                                                                 where string.Compare(term.Text, _selectedTerm, true) == 0
                                                                                 select term).FirstOrDefault());
            if (ReadOnly)
                ddlTaxonomyField.Enabled = false;
            
        }


        #endregion

        #region Public Methods

        public void Required(bool isRequired)
        {
            if (isRequired)
                ddlTaxonomyField.Required(ExceptionConstants.ERROR_DDL_OBLIGATORIO,"ValBtnAddRadicado", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        public void Required(bool isRequired, string ValidationGroup)
        {
            if (isRequired)
                ddlTaxonomyField.Required(ExceptionConstants.ERROR_DDL_OBLIGATORIO, ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        public void EnabledFuncion(bool isRequired)
        {
            ddlTaxonomyField.Enabled = isRequired;
        }

        public void SelectedIndexTerm(string Term)
        {
            if (!string.IsNullOrEmpty(Term))
                ddlTaxonomyField.SelectedIndex = ddlTaxonomyField.Items.IndexOf((from term in ddlTaxonomyField.Items.OfType<ListItem>()
                                                                                 where string.Compare(term.Text, _selectedTerm, true) == 0
                                                                                 select term).FirstOrDefault());
        }

        #endregion

    }
}
